from bson import ObjectId
from pymongo import MongoClient, UpdateOne
from conf.settings import CLOUDMONITOR_MONGODB, CLOUDMONITOR_DB

target = ""

conn = MongoClient(target)
target_db = conn[CLOUDMONITOR_DB]
conn_1 = MongoClient(CLOUDMONITOR_MONGODB)
local_db = conn_1[CLOUDMONITOR_DB]


def main():
    collection_tuple = ('assets', 'tasks', 'url_blacklist', 'url_blacklist_setting')
    # collection_tuple = ('assets', 'alerts', 'siteportraits', 'resultnumbers')

    job_id_list = [
        ObjectId("623997edacdbd3001107feac"),
        ObjectId("6239980eacdbd3001107feb2"),
        ObjectId("62393f2dd53ea50011301ef1"),
        ObjectId("62399620a10051001130a44c"),
        ObjectId("623a88ceb0d6c80011fbd981"),
    ]
    job_query = {'_id': {'$in': job_id_list}}

    insert_dict = {'jobs': [UpdateOne({'_id': item['_id']}, {'$set': item}, upsert=True) for item in target_db['jobs'].find(job_query)]}
    query = {'jobId': {'$in': job_id_list}}
    for collection in collection_tuple:
        insert_dict[collection] = [
            UpdateOne({'_id': item['_id']}, {'$set': item}, upsert=True) for item in target_db[collection].find(query)
        ]

    for k, v in insert_dict.items():
        if v:
            local_db[k].bulk_write(v)

    print("Succeed")


if __name__ == '__main__':
    main()
